package graphicinterface;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import bookworm.Book;

/**
 * this class will be a JPanel with a book cover, 
 * stars representing the average rating of the book(based on review from it)
 * and one "cute character" right beside the cover representing if the
 * book had a good rating or not
 * @author Mina
 *
 */
public class JPanelWithBookStarsAndCharacter extends JPanel implements MouseListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Book book;
	public JPanelWithBookStarsAndCharacter(Book book)
	{
		this.book = book;
		this.createPanelWithBookAndStarsAndCharacter();
		this.addMouseListener(this);
	}
	
	private void createPanelWithBookAndStarsAndCharacter()
	{
		this.setBackground(new Color(255,131,137));
		
		JPanel panelWithBookTitle = new JPanel();
		panelWithBookTitle.setBackground(new Color(255,131,137));
		String bookName = book.getBookName();
		String redimesionedBookName = redimensionBookName(bookName);
		JLabel labelBookTitle = TextLabelCreator.createStyledTextlabel(redimesionedBookName);
		panelWithBookTitle.add(labelBookTitle);
		
		GridBagLayout layout = new GridBagLayout();
		this.setLayout(layout);
		GridBagConstraints grid = new GridBagConstraints();
		
		grid.gridwidth = 0;
		grid.gridx = 0;
		grid.gridy = 0;
		this.add(panelWithBookTitle, grid);
		
		JPanel panelWithCuteCharacter = createPanelWithCuteCharacter(book);
		grid.gridwidth = 1;
		grid.gridy = 1;
		this.add(panelWithCuteCharacter, grid);
		
		JPanel panelBookCover = createPanelWithBookCover(book);
		grid.gridx = 1;
		this.add(panelBookCover, grid);
		
		int ratingOfBook = book.getAverageRatingFromABook();
		JLabel labelWithStars = CreatesLabelWith5StarRating.createLabelWithStars(ratingOfBook);
		JPanel panelWithStars = new JPanel();
		panelWithStars.setBackground(new Color(255,131,137));
		panelWithStars.add(labelWithStars);
		
		grid.gridx = 0;
		grid.gridy = 2;
		grid.gridwidth = 2;
		this.add(panelWithStars, grid);
	}
	
	private JPanel createPanelWithCuteCharacter(Book book)
	{
		int ratingOfBook = book.getAverageRatingFromABook();
		ImageIcon iconWithCuteCharacter;
		if(ratingOfBook < 3)
		{
			//bad rating book
			iconWithCuteCharacter = 
					CreatesImageIcon.createImageIcon("images/chibicrying.png", "crying character");
		}
		else
		{
			//good rating book
			iconWithCuteCharacter = 
					CreatesImageIcon.createImageIcon("images/chibihappy.png", "happy character");
		}
		
		JLabel labelWithCuteCharacter = new JLabel(iconWithCuteCharacter);
		JPanel panelWithCuteCharacter = new JPanel();
		panelWithCuteCharacter.setBackground(new Color(255,131,137));
		panelWithCuteCharacter.add(labelWithCuteCharacter);
		return panelWithCuteCharacter;
	}
	
	private JPanel createPanelWithBookCover(Book book)
	{
		String addressOfBookCover = book.getUrlToCover();
		ImageIcon iconBookCover =
				GetsBookCoverByImageAddress.getBookCover(addressOfBookCover);
		JLabel labelBookCover = new JLabel(iconBookCover);
		JPanel panelBookCover = new JPanel();
		panelBookCover.setBackground(new Color(255,131,137));
		panelBookCover.add(labelBookCover);
		return panelBookCover;
	}
	
	/**
	 * if a name of a book is very long, we'll put some dots on it's final.
	 * Example: "the short second life of bree tanner" turns to "the short secon..."
	 * @param name
	 * @return
	 */
	private String redimensionBookName(String name)
	{
		String redimensionedName = "";
		int lengthOfRedimensionedName = 0;
		int nameIterator = 0;
		
		while(lengthOfRedimensionedName < 15 && nameIterator < name.length())
		{
			char oneChar = name.charAt(nameIterator);
			String charInString = String.valueOf(oneChar);
			redimensionedName = redimensionedName + charInString;
			lengthOfRedimensionedName = lengthOfRedimensionedName + 1;
			nameIterator = nameIterator + 1;
		}
		
		if(nameIterator < name.length())
		{
			return redimensionedName + "...";
		}
		else
		{
			return redimensionedName;
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) 
	{
		WindowShowReviewsFromBook interfaceReviewsFromBook = 
									new WindowShowReviewsFromBook(this.book);
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
